
from django.shortcuts import render
from .models import Employ,Department,scorer,question
from .department import *


i=1
GENDER_CHOICES_DEPART={
		'管理员':0,
		'书记':1,
		'院长':2,
		'院领导':3,
		'部门负责人':4,
		'其他中层':5,
		'专职教师':6,
		'辅导员':7,
		'普通职工':8,
}
GENDER_CHOICES_DEPARTnumber=dict(zip(GENDER_CHOICES_DEPART.values(),GENDER_CHOICES_DEPART.keys()))

manage_depart={'王虎':['保卫部','经营中心','老校区办公室','人力资源处','图书馆','现代教育技术中心','校园中心','饮食中心','资产管理处','综合科','财务点'],
               '李浩':['党委宣传部','党委学生工作部','党委组织部','教育科学学院','工会办公室','经济与管理学院','理工学院','人文与艺术学院','数学与计算机科学学院','团委','心理健康教育与咨询中心','政法学院'],
               '张长伟':['教师发展与教学研究中心','教务处','就业创业服务中心','招生办公室'],
               '金绪泽':['党政办公室','发展规划处'],}



class userinfor:
    '''对象类'''
    def __init__(self,people,value):
        self.people=people
        self.value=value


#其他 普通职工 院长 书记 主管院领导 其他领导-除了主管的其他三位领导
#ast.literal_eval(strb)  strb='{asd:asd}'
def getsxl():
    id='xl0058';
    y = Employ.objects.filter(employ_auth=GENDER['院长'])
    s = Employ.objects.filter(employ_auth=GENDER['书记'])
    dqw_var_obj_list = Employ.objects.filter(employ_ID='083005');
    cbg_var_obj_list = Employ.objects.filter(employ_ID='121051');
    zcw_var_obj_list = Employ.objects.filter(employ_ID='161010');
    wh_var_obj_list = Employ.objects.filter(employ_ID='583011');
    lh_var_obj_list = Employ.objects.filter(employ_ID='083032');
    Middle_var_obj_list = Employ.objects.filter(employ_ismiddle=1).exclude(employ_ID=id)

    total_value=0;
    total_value+=getsomebody(y,id)*0.2;
    total_value+=getsomebody(s, id)*0.2;
    total_value +=getsomebody(dqw_var_obj_list, id)*0.3;
    total_value +=getsomebody(cbg_var_obj_list, id)*0.3;
    total_value +=getsomebody(zcw_var_obj_list, id)*0.2;
    total_value +=getsomebody(wh_var_obj_list, id)*0.2;
    total_value +=getsomebody(lh_var_obj_list, id)*0.2;
    total_value +=getsomebody(Middle_var_obj_list, id)*0.1;

    return total_value;

def getsomebody(var_object_list,id):
    var_total = 0
    value = 0
    # print("start_base_value")
    if len(var_object_list) == 0:
        return 0
    for i in var_object_list:
        value = scorer.objects.filter(scorer_appraiser=i.employ_ID, scorer_appraisered=id)
        if (len(value)):
            var_total += value.first().scorer_value
    return var_total / len(var_object_list)


def getgrade(user):
    grade_dict_object = Department.objects.filter(Department_depart=user.employ_depart,employ_auth=user.employ_auth)
    if(len(grade_dict_object)!=1 ):
        return None
    grade_dict='{'+grade_dict_object[0].calculate+'}'
    grade_dict=eval(grade_dict)
    depart=user.employ_depart
    '''
    part=0
    for i in grade_dict:
        part+=grade_dict[i]
    if(round(part,2)!=1):
        print(grade_dict_object,part,'\n',grade_dict)
        '''
    class_obj=depart_class_list[depart](user.employ_ID,depart,user.employ_auth,grade_dict_object[0].Department_manage_name,grade_dict)
    return class_obj.soft_auth()

def get_object_or_error(request,id,password):
    print(id,password)
    try:
        print(1)
        user = Employ.objects.get(employ_ID=id)
    except:
        return render(request, 'empoll/error.html', {'error_msg': '工号不存在！'})
    else:
        print(user.employ_login)
        if user.employ_login == password:
            return user
        else:
            return render(request, 'empoll/error.html', {'error_msg': '密码错误！'})



#判断个人评分超过95分的是否占所评论人员的50%
def judge_is_half(use,apped,value,belong_depart):
    count=0;
    total=0;
    if(value>=95):
        list = scorer.objects.filter(scorer_appraiser=use.employ_ID)
        for i in list:
            if Employ.objects.filter(employ_ID=i.scorer_appraisered).first().employ_depart in manage_depart[belong_depart] :
                count=count+1;
        list2 = Employ.objects.all().exclude(employ_ID=use.employ_ID)
        for i in list2:
            if i.employ_depart in manage_depart[belong_depart]:
                total=total+1;
        if(int(total/2)<=count):
            return False
    return True
'''
        if(use.employ_auth>=1 and use.employ_auth<=2):
            if len(scorer.objects.filter(scorer_appraiser=use.employ_ID,scorer_value__gte=95))> len(Employ.objects.filter(employ_auth__lte=8, employ_auth__gte=4).exclude(employ_ID=use.employ_ID))/2:
                return False
        if(use.employ_auth==3 and use.employ_name != '段其旺'):
            count=0
            count_95_belong=0
            count_95_nobelong=0
            list = scorer.objects.filter(scorer_appraiser=use.employ_ID)
            list2=Employ.objects.all()
            belong_count=0
            for i in list2:
                if( i.employ_depart in manage_depart[use.employ_name]):
                    belong_count+=1
            no_belong_count=len(list2)-belong_count
            for i in list:
                be=Employ.objects.filter(employ_ID= i.scorer_appraisered)
                if be.employ_depart in manage_depart[use.employ_name]:
                    count+=1
                    if i.scorer_value >= 95:
                        count_95_belong+=1
                else:
                    if i.scorer_value >= 95:
                        count_95_nobelong+=1

            if( user2.employ_depart in manage_depart[use.employ_name]):
                if  count_95_belong > belong_count / 2:
                    return False
            else:
                if count_95_nobelong > no_belong_count / 2:
                    return False
        return True
    else:
        return True
'''
'''
if(value>=95):
        user2=Employ.objects.filter(employ_ID=apped)
        if(use.employ_auth>=1 and use.employ_auth<=2):
            if len(scorer.objects.filter(scorer_appraiser=use.employ_ID,scorer_value__gte=95))> len(Employ.objects.filter(employ_auth__lte=8, employ_auth__gte=4).exclude(employ_ID=use.employ_ID))/2:
                return False
        if(use.employ_auth==3 and use.employ_name != '段其旺'):
            count=0
            count_95_belong=0
            count_95_nobelong=0
            list = scorer.objects.filter(scorer_appraiser=use.employ_ID)
            list2=Employ.objects.all()
            belong_count=0
            for i in list2:
                if( i.employ_depart in manage_depart[use.employ_name]):
                    belong_count+=1
            no_belong_count=len(list2)-belong_count
            for i in list:
                be=Employ.objects.filter(employ_ID= i.scorer_appraisered)
                if be.employ_depart in manage_depart[use.employ_name]:
                    count+=1
                    if i.scorer_value >= 95:
                        count_95_belong+=1
                else:
                    if i.scorer_value >= 95:
                        count_95_nobelong+=1

            if( user2.employ_depart in manage_depart[use.employ_name]):
                if  count_95_belong > belong_count / 2:
                    return False
            else:
                if count_95_nobelong > no_belong_count / 2:
                    return False
        return True
    else:
        return True
    '''



#获取不同的管理者所管理的人员
def getleader_manage_people(id):
    all_user=Employ.objects.filter(employ_auth__lte=8, employ_auth__gte=4).exclude(employ_ID=id)
    '''name = []
    for i in manage_depart:
        name.append(i)'''
    wang_list=[]
    li_list=[]
    zhang_list=[]
    jin_list=[]
    wang_no_list=[]
    li_no_list=[]
    zhang_no_list=[]
    jin_no_list=[]
    has_appr = scorer.objects.filter(scorer_appraiser=id)

    for i in has_appr:
        objuser=Employ.objects.get(employ_ID=i.scorer_appraisered)
        blend = userinfor(objuser, i.scorer_value)
        if(objuser.employ_depart in manage_depart['王虎']):
            wang_no_list.append(blend)
        elif(objuser.employ_depart in manage_depart['李浩']):
            li_no_list.append(blend)
        elif(objuser.employ_depart in manage_depart['张长伟']):
            zhang_no_list.append(blend)
        else:
            jin_no_list.append(blend)

    for obj in all_user:
        if(obj.employ_depart in manage_depart['王虎']):
            wang_list.append(obj)
        elif(obj.employ_depart in manage_depart['李浩']):
            li_list.append(obj)
        elif(obj.employ_depart in manage_depart['张长伟']):
            zhang_list.append(obj)
        else:
            jin_list.append(obj)
    return [wang_list,li_list,zhang_list,jin_list],[wang_no_list,li_no_list,zhang_no_list,jin_no_list]


def _get_queryset(klass):
    """
    Return a QuerySet or a Manager.
    Duck typing in action: any class with a `get()` method (for
    get_object_or_404) or a `filter()` method (for get_list_or_404) might do
    the job.
    """
    # If it is a model class or anything else with ._default_manager
    if hasattr(klass, '_default_manager'):
        return klass._default_manager.all()
    return klass


def getall(request,id):
    try:
        user = Employ.objects.get(employ_ID=id)
        has=user.employ_has.split(',')
    except:
        return render(request, 'empoll/error.html', {'error_msg': '出错请联系管理员'})
    else:
        return has
